package dianmobile.eistarandroid.ui.fragments;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.zcw.togglebutton.ToggleButton;

import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.ui.activities.ManageAccountActivity;
import dianmobile.eistarandroid.utils.SettingInfoManager;

public class SettingFragment extends Fragment {
    private ToggleButton tgBtnShowPicture;
    private ToggleButton tgBtnShowOnlyUnread;
    private ToggleButton tgBtnAutoUpdate;
    private SettingInfoManager settingInfoManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_setting, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        settingInfoManager = new SettingInfoManager(getActivity());

        setToggleButton();
        setSpinner();
        setClickListener();
    }

    private void setToggleButton() {
        tgBtnShowPicture = (ToggleButton)getActivity().findViewById(R.id.tgBtn_show_picture);
        tgBtnShowOnlyUnread = (ToggleButton)getActivity().findViewById(R.id.tgBtn_show_only_unread);
        tgBtnAutoUpdate = (ToggleButton)getActivity().findViewById(R.id.tgBtn_auto_update);

        tgBtnShowPicture.toggle();
        if (settingInfoManager.isAutoShowPicture()) {
            tgBtnShowPicture.setToggleOn();
        } else {
            tgBtnShowPicture.setToggleOff();
        }
        tgBtnShowPicture.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                settingInfoManager.setIsAutoShowPicture(on);
                if (on) {
                    tgBtnShowPicture.setToggleOn();
                } else {
                    tgBtnShowPicture.setToggleOff();
                }
            }
        });

        tgBtnShowOnlyUnread.toggle();
        if (settingInfoManager.isOnlyShowUnread()) {
            tgBtnShowOnlyUnread.setToggleOn();
        } else {
            tgBtnShowOnlyUnread.setToggleOff();
        }
        tgBtnShowOnlyUnread.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                settingInfoManager.setIsOnlyShowUnread(on);
                if (on) {
                    tgBtnShowOnlyUnread.setToggleOn();
                } else {
                    tgBtnShowOnlyUnread.setToggleOff();
                }
            }
        });

        tgBtnAutoUpdate.toggle();
        if (settingInfoManager.isAutoCheckUpdate()) {
            tgBtnAutoUpdate.setToggleOn();
        } else {
            tgBtnAutoUpdate.setToggleOff();
        }
        tgBtnAutoUpdate.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                settingInfoManager.setIsAutoCheckUpdate(on);
                if (on) {
                    tgBtnAutoUpdate.setToggleOn();
                } else {
                    tgBtnAutoUpdate.setToggleOff();
                }
            }
        });
    }

    private void setSpinner() {
        Spinner spTimeInterval = (Spinner)getActivity().findViewById(R.id.sp_time_interval);
        String[] timeIntervalItem = getResources().getStringArray(R.array.time_interval);
        ArrayAdapter<String> timeIntervalAdapter = new ArrayAdapter<>(getActivity(),
                android.R.layout.simple_spinner_item, timeIntervalItem);
        timeIntervalAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spTimeInterval.setAdapter(timeIntervalAdapter);
        spTimeInterval.setSelection(settingInfoManager.getTimeInterval(), true);
        spTimeInterval.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                settingInfoManager.setTimeInterval(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        Spinner spPictureQuality = (Spinner)getActivity().findViewById(R.id.sp_picture_quality);
        String[] pictureQualityItem = getResources().getStringArray(R.array.picture_quality);
        ArrayAdapter<String> pictureQualityAdapter = new ArrayAdapter<>(getActivity(),
                android.R.layout.simple_spinner_item, pictureQualityItem);
        pictureQualityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spPictureQuality.setAdapter(pictureQualityAdapter);
        spPictureQuality.setSelection(settingInfoManager.getUploadPictureQuality());
        spPictureQuality.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                settingInfoManager.setUploadPictureQuality(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void setClickListener() {
        getActivity().findViewById(R.id.btn_account_management).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ManageAccountActivity.class);
                startActivity(intent);
            }
        });

        getActivity().findViewById(R.id.btn_feedback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "click on feedback", Toast.LENGTH_SHORT).show();
            }
        });

        getActivity().findViewById(R.id.btn_about).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "click on about us", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
